<?php
function global_message()
{ 
	$CI = &get_instance();
	if ($CI->session->flashdata('error_message'))
	{ 
		echo global_message_output($CI->session->flashdata('error_message') , 'error_message' ) ; 
	}
	if ($CI->session->flashdata('success_message'))
	{ 
		echo global_message_output($CI->session->flashdata('success_message') , 'success_message' ) ; 
		
	}
	if ($CI->session->flashdata('warning_message'))
	{ 
		echo global_message_output($CI->session->flashdata('warning_message') , 'warning_message' ) ; 
		
	}
	// this is because to show the error when there is error in the insertion in the database. We cannot use session beaucas it does not support the object as the value. We we used the post data again as the source to conserve the data. 
	if ($CI->input->post('error_message')) 
	{ 
		echo global_message_output($CI->input->post('error_message') , 'error_message' ) ; 
	}     
}

function global_message_output($msg,$var_name)
{ 
	if (is_string($msg))
	{ 
		return    "<div class='".$var_name."' onclick='javascript:$(this).fadeOut(1000)'>".$msg."</div>"; 		
	}
	else if (is_array($msg))
	{ 
		$message = "<ul class= '".$var_name."' style='list-style:disc' onclick='javascript:$(this).fadeOut(1000)'> "; 
		
		foreach ($msg as $ms) 
		{ 
			$message .= "<li>" . $ms . "</li>" ; 
		} 
		$message .= "</ul>"; 
		return $message; 
	}  
} 

function debug_array($array)
{
	echo "<pre>";
	print_r($array);
	echo "</pre>";
}

function generate_category_code($parent_category_id=false)
{
	$CI = &get_instance();
	
	$codes = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
	
	if($parent_category_id)
	{
		//$parent_code_r = $CI->general_db_model->getById('tbl_publication_category', 'id', $parent_category_id, 'code');
		//$parent_code = $parent_code_r->code;
		
		$last_code_r = $CI->general_db_model->getLast('tbl_publication_category', 'code', 'id', 'parent_id = '.$parent_category_id);
		
		if($last_code_r)
			$last_code = $last_code_r->code;
		else
			$last_code = 0;
			
		return (int)$last_code + 1;	
		
	}
	else
	{
		$last_code_r = $CI->general_db_model->getLast('tbl_publication_category', 'code', 'id', 'parent_id = 0');
		if($last_code_r)
		{
			$last_code = $last_code_r->code;
			$key = array_search($last_code, $codes);
			$code = $codes[$key+1];
		}
		else
			$code = $codes[0];
				
		return $code;
	}
}

function generate_publication_code($cat_id, $sub_cat_id)
{
	$CI = &get_instance();
	
	$last_code_r = $CI->general_db_model->getLast('tbl_publication_info', 'coding', 'id', 'category_id = '.$cat_id.' and sub_category_id = '.$sub_cat_id);
	
	if($last_code_r)
		return $last_code_r->coding;
	else
		return false;
}

function sub_category($category_id, $sub_category=NULL)
{
	$CI = &get_instance();
	$sub_categories = $CI->general_db_model->getAll('tbl_publication_category', 'category_name', 'parent_id = '.$category_id);
	$output = '<select name="sub_category_id" id="sub_category_id" onchange="generate_code();" class="">';
	$output.= '<option value="0">None</option>';
	foreach($sub_categories as $sub_cat)
	{
		$output.= '<option value="'.$sub_cat->id.'" ';
		if($sub_category)
		{
			if($sub_category == $sub_cat->id)
				$output.= 'selected="selected"';
		}
		$output.= '>'.$sub_cat->category_name.'</option>';	
	}
	$output.= '</select>';
	return $output;
}

function create_pagination($totalRows, $uri_segment = 4, $per_page = NULL)
{
	$CI = &get_instance();
	$CI->load->library('pagination');
	if (preg_match('/(.*)\/page\/[0-9]+/',current_url()))
	{ 	
		$url = preg_replace('/\/page\/[0-9]+/','',current_url());
		$array['base_url'] = $url.'/page'; 
	} 
	else 
	{ 
		$array['base_url'] = current_url()."/page"; 
	} 
	
	//$array['base_url'] = site_url('admin/media/lists/page/');
	$array['total_rows'] = $totalRows ;
	$array['uri_segment'] = $uri_segment;
	$array['first_link'] = 'First';
	$array['last_link'] = 'Last';
	$array['next_link'] = 'Next';
	$array['prev_link'] = 'Prev';
	if($per_page)
		$array['per_page'] = $per_page;
	else
		$array['per_page'] = $CI->config->item('per_page');

	$CI->pagination->initialize($array);
	// if some rows is deleted then there is no rows for that particular page then redirect to previous page. 
	
	if ($totalRows == $CI->uri->segment($uri_segment) and ereg('/(.*)/page/[0-9]+',current_url()) ) 
	{ 
		$CI->session->keep_flashdata('error_message');
		$CI->session->keep_flashdata('success_message');
		$uri_value = ($totalRows -  $array['per_page']); 
		$url = $array['base_url'].'/'. $uri_value; 
		if ($uri_value== 0 ) 
		{ 
			$url = eregi_replace('/page/[0-9]+','',current_url());
		} 
		redirect( $url ); 
	} 
}

function upload_file($file, $target, $file_type, $prev_file = NULL, $max_size = '102400')
{

 $CI = &get_instance();
 $config = array();
 $config['upload_path'] = $target;
 $config['allowed_types'] = $file_type;
 $config['max_size'] = $max_size;

 $CI->load->library('upload_file');
 $CI->upload_file->initialize($config);

 if($CI->upload_file->do_upload($file))
 {
  if($prev_file)
  {
   if(is_file($target.$prev_file))
    @unlink($target.$prev_file);
  }

  $data = $CI->upload_file->data();
  $file = $data['file_name'];

  return $file;
 }
 else
 {
  $CI->session->set_flashdata('error_message', $CI->upload_file->display_errors());
  return false;
  //return $CI->upload->display_errors();
 }
 }
 
function upload_image($image, $target, $thumb = array('dest' => '', 'size' => array('w' => 257, 'h' => 218), 'ratio' => true), $prev_img = NULL)
{
    //echo $target;exit;
    //$target = 'C:/wamp/www//joomlaTest/publication/uploads/publication_images/';
	$CI = &get_instance();
	initialize_upload($target);
	if($CI->upload->do_upload($image))
	{
		if($prev_img)
		{
			if(is_file($target.$prev_img))
				@unlink($target.$prev_img);
		}
		
		$data = $CI->upload->data();
		$image = $data['file_name'];
		$image_path = $data['full_path'];
		$image_name = $data['raw_name'];
		$image_ext = $data['file_ext'];
		
		if($thumb)
		{
			//$thumb_size = array('w' => 200, 'h' =>220);
			if($thumb['dest'])
				$dest = $thumb['dest'];
			else
				$dest = $target;
			create_thumb($image_path, $dest.$image, $thumb['size'], $thumb['ratio']);
		}
		return $image;
	}
	else
	{
		$CI->session->set_flashdata('error_message', $CI->upload->display_errors());
		return false;
		//return $CI->upload->display_errors();
	}
	
}

function initialize_upload($path, $max_size = '10240', $max_width = '2560', $max_height='1920')
{
	$CI = &get_instance();
	$config = array();
	$config['upload_path'] = $path;
	$config['allowed_types'] = 'gif|jpg|png';
	$config['max_size']	= $max_size;
	$config['max_width']  = $max_width;
	$config['max_height']  = $max_height;
	
	$CI->load->library('upload', $config);

}

function create_thumb($src, $dest, $size, $ratio = true)
{
	$CI = &get_instance();
	
	$config['image_library'] = 'gd2';
	$config['source_image'] = $src;
	$config['new_image']    = $dest;
	$config['create_thumb'] = TRUE;
	if($ratio)
		$config['maintain_ratio'] = TRUE;
	else
		$config['maintain_ratio'] = FALSE;
		
	$config['thumb_marker'] = '';	
		
	$config['width'] = $size['w'];
	$config['height'] = $size['h'];
	
	$CI->load->library('image_lib');
	$CI->image_lib->initialize($config); 
	
	$CI->image_lib->resize();

}

function quantity($publ_id, $type, $select='*')
{
	$CI = &get_instance();
	$CI->load->model('publication_model');
	return $CI->publication_model->get_quantity($publ_id, $type, $select);
}

function get_image($path, $img = NULL)
{
	if($img)
		return $path.$img;
	else
		return $path.'no_avatar.gif';
}

function action_link($type, $id, $query=NULL)
{
	$CI = &get_instance();

	$segment_array  =  $CI->uri->segment_array(); 
	$keys = array_keys($segment_array, 'page');
	if (count($keys))
	{ 
		array_splice($segment_array, count($segment_array)-2, 0, array($type,$id));
	}
	else 	
	{ 
		array_splice($segment_array, count($segment_array), 0, array($type,$id)); 
	}
	
	$url =  site_url($segment_array);  
	
	$query = $_SERVER['QUERY_STRING'];
	if($query)
		$url = $url.'?'.$query;

	return $url;
}

function filter_url($type, $query=NULL)
{ 
	$CI = &get_instance();
	$segment_array  =  $CI->uri->segment_array(); 
	$keys = array_keys($segment_array, $type);
	
	$total = count($segment_array); 
	if (count($keys))
	{ 
		array_splice($segment_array, $keys[0]-1 ,2 ); 
		
	}
	
	$url =  site_url($segment_array);
	
	$query = $_SERVER['QUERY_STRING'];
	if($query)
		$url = $url.'?'.$query;
	return $url;
}

function delete_file($file)
{
	if(file_exists($file))
		@unlink($file);
}

function generate_delay_date($unformatted_end_date){?>
    <li>
         <span class="small padding_0left">Delay-days:</span>
         <strong><?php
            $today = strtotime(date('Y-m-d'));
            $endDate = strtotime($unformatted_end_date);
            $daysDiff = floor(($today-$endDate)/(60*60*24));
            $style = ($daysDiff>0)? 'color:red;':'';
            //$daysDiff = round(abs($endDate-$today)/60/60/24);
            echo "<span style='$style'>$daysDiff days</span>";?>
         </strong>
     </li>
<?php }
function debug($array){
    echo "<pre>";
    print_r($array);
    echo "</pre>";
}
function is_super_admin()
{
	$CI = & get_instance();
	if($CI->session->userdata('user_group') == 'super_admin')
		return true;
	else
		return false;
}

function user_id()
{
	$CI = & get_instance();
	return $CI->session->userdata('pub_admin_login_id');
}




?>